package com.zjw.manager;

import com.zjw.entity.User;

import java.io.*;
import java.util.Scanner;

public class Manager {
    private Scanner sc = new Scanner(System.in);
    private User[] users = new User[50];
    private final String TEL_PATH = "F:/tel.data";

    private void saveTel(){
        try {
            FileOutputStream out = new FileOutputStream(TEL_PATH);
            PrintWriter pw = new PrintWriter(out,true);
            for (User user : users) {
                if (user != null)
                    pw.println(user.getName() + "\t\t" + user.getTel());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    private void readTel(){
        File f = new File(TEL_PATH);
        if(!f.exists()){
            return;
        }
        try {
            FileReader in = new FileReader(f);
            BufferedReader br = new BufferedReader(in);
            String str = br.readLine();
            while (str != null){
                String[] us = str.split("\t\t");
                if(us.length == 2){
                    User u = new User();
                    u.setName(us[0]);
                    u.setTel(us[1]);
                    for (int i = 0; i < users.length; i++) {
                        if (users[i] == null) {
                            users[i] = u;
                            break;
                        }
                    }
                }
                str = br.readLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private void add() {
        User user = new User();
        System.out.print("姓名:");
        user.setName(sc.next());
        System.out.print("电话号码:");
        user.setTel(sc.next());
        for (int i = 0; i < users.length; i++) {
            if (users[i] == null) {
                users[i] = user;
                break;
            }
        }
        System.out.println("新增成功");
        saveTel();
    }

    private void modify(){
        System.out.print("姓名:");
        String name = sc.next();
        System.out.print("请输入新的电话号码:");
        String phone = sc.next();
        for (int i = 0; i < users.length; i++) {
            if (users[i] != null && users[i].getName().equals(name)) {
                users[i].setTel(sc.next());
            }
        }
        System.out.println("修改成功");
        saveTel();
    }

    private void show(){
        System.out.println("姓名\t电话");
        System.out.println("=================");
        for (User user : users) {
            if (user != null)
                System.out.println(user.getName() + "\t\t" + user.getTel());
        }
        System.out.println("删除成功");
        saveTel();
    }

    private void remove(){
        System.out.print("姓名:");
        String name = sc.next();
        for (int i = 0; i < users.length; i++) {
            if (users[i] != null && users[i].getName().equals(name)) {
                users[i] = null;
            }
        }
        System.out.println("删除成功!");
        saveTel();
    }

    public void start() {
        readTel();
        System.out.println();
        while (true) {
            System.out.println("电话薄");
            System.out.println("===========================");
            System.out.println("1.新增");
            System.out.println("2.修改");
            System.out.println("3.查看");
            System.out.println("4.删除");
            System.out.println("5.退出");
            System.out.println("===========================");
            System.out.print("请输入功能编号：");

            String str = sc.next();
            if (str.equals("1")) {
                add();
            } else if (str.equals("2")) {
                modify();
            } else if (str.equals("3")) {
                show();
            } else if (str.equals("4")) {
                remove();
            } else if (str.equals("5")) {
                System.out.println("成功退出!");
                break;
            } else {
                System.out.println("请重新输入");
                break;
            }
        }
    }
}
